import { generateHighlightNode } from '@/utils/bizUtils';

// 渲染计量单位的inputNumber
const renderUnitInput = (h, row, presentKey) => {
  return <el-input-number
    size="mini"
    controls-position="right"
    value={row[presentKey]}
    max={3}
    min={0}
    precision={0}
    onChange={(val) => { row[presentKey] = val; }}
  />;
}

// 渲染转化率的inputNumber
const renderRatioInput = (h, row, presentKey) => {
  return <el-input-number
    size="mini"
    controls={false}
    value={row[presentKey]}
    onChange={(val) => { row[presentKey] = val; }}
  />;
}

// 获取现值render
const getPresentRender = (presentKey, renderType) => {
  const title = '现值';
  const presentByRender = {
    'unitInput': renderUnitInput,
    'ratioInput': renderRatioInput
  };
  const isInput = renderType === 'unitInput' || renderType === 'ratioInput';
  const renderFn = presentByRender[renderType];
  if (renderFn) {
    return {
      title,
      width: isInput ? 160 : undefined,
      render: (h, { row }) => renderFn(h, row, presentKey)
    };
  } else {
    return { title: '现值', key: presentKey };
  }
}

// 获取children的render
export const getChildrenRender = (originalKey, presentKey, renderType) => {
  const children = [
    { title: '原值', key: originalKey },
    getPresentRender(presentKey, renderType)
  ];
  return children;
}

// 获取查看children的render
export const getViewChildrenRender = (originalKey) => {
  const children = [
    { title: '原值', key: originalKey },
    {
      title: '现值',
      render: (h, { row }) => {
        return generateHighlightNode(h, row, originalKey);
      }
    }
  ];
  return children;
}
